<template>
  <view class="content">
    <!-- #ifdef APP-PLUS -->
    <view v-if="!fingerPrint || !isSupportSoter" class="input-box">
      <uni-easyinput type="password" :trim="true" :clearSize="0" adjust-position placeholder="请输入密码" v-model="my_password" />
      <button class="base-button btn" @click="checkPassword">确认</button>
    </view>
    <!-- #endif -->
    <!-- #ifdef H5 -->
    <view v-if="usePassword" class="input-box">
      <uni-easyinput type="password" :trim="true" :clearSize="0" adjust-position placeholder="请输入密码" v-model="my_password" />
      <button class="base-button btn" @click="checkPassword">确认</button>
    </view>
    <!-- #endif -->
  </view>
</template>

<script setup>
import { ref, watch } from "vue";
import { storeToRefs } from "pinia";
import useSafeStore from "@/store/modules/safe.js";
import { onShow } from "@dcloudio/uni-app";
// #ifdef APP-PLUS
import { IsSupportSoter, Authentication } from "@/utils/authentication.js";
// #endif

let safeStore = useSafeStore();
let { fingerPrint, usePassword } = storeToRefs(safeStore);

let my_password = ref("");

let isAuth = ref(false);

onShow(() => {
  // #ifdef H5
  if (!usePassword.value) uni.navigateBack();
  // #endif
  // #ifdef APP-PLUS
  if (!fingerPrint.value && !usePassword.value) uni.navigateBack();
  if (fingerPrint.value) {
    completeAuthenticationFun();
  }
  // #endif
});

// #ifdef APP-PLUS
// 判断是否支持指纹识别
let isSupportSoter = ref(false);
// 判断是否支持指纹识别
const IsSupportSoterFunc = async () => {
  isSupportSoter.value = await IsSupportSoter();
};
// 验证指纹识别
const authenticationFun = async () => {
  isAuth.value = await Authentication();
};

// 完整校验流程
const completeAuthenticationFun = async () => {
  await IsSupportSoterFunc();
  if (isSupportSoter.value) await authenticationFun();
};
// #endif

// 校验密码
const checkPassword = () => {
  isAuth.value = safeStore.checkpassword(my_password.value);
  if (!isAuth.value)
    uni.showToast({
      title: "校验错误",
      icon: "error",
      duration: 2000,
    });
};

watch(
  () => isAuth.value,
  (newV) => {
    if (newV) {
      safeStore.changeIsAuth();
      uni.navigateBack();
    }
  }
);
</script>

<style lang="scss" scoped>
.content {
  display: flex;
  justify-content: center;
  padding-top: 20vh;
  .input-box {
    width: 400rpx;
  }
  .btn {
    margin-top: 20px;
  }
}
</style>
